package com.borrowed.book.membercard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.borrowed.book.book.constant.BookConstant;
import com.borrowed.book.membercard.entity.SMemberCard;
import com.borrowed.book.membercard.entity.SMembercardUser;
import com.borrowed.book.membercard.service.SMembercardUserService;
import com.borrowed.book.membercard.mapper.SMembercardUserMapper;
import com.borrowed.book.system.util.DayUtil;
import com.borrowed.book.user.domain.SUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
*会员卡服务
*/
@Service
public class SMembercardUserServiceImpl extends ServiceImpl<SMembercardUserMapper, SMembercardUser> implements SMembercardUserService{
    /**
     * 查询用户绑定的有效的会员卡信息
     * @param openId
     * @return
     */
    @Override
    public List<SMembercardUser> getMembercardUser(String openId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("open_id",openId);
        queryWrapper.gt("end_time",new Date());
        queryWrapper.orderByDesc("end_time");
        List<SMembercardUser> data=this.list(queryWrapper);
        return data;
    }

    /**查询用户的会员卡是否在有效期内
     * @param sMembercardUser
     */
    @Override
    public boolean ValidityPeriod(SMembercardUser sMembercardUser) {
        boolean flag=false;
        Calendar now=Calendar.getInstance();
        Date nowDate=now.getTime();
        flag= DayUtil.timeBetweenCapare(nowDate,sMembercardUser.getStartTime(),sMembercardUser.getEndTime());
        return flag;
    }

    /**
     * 新增用户会员卡信息
     *
     * @param sMemberCard
     * @param openId
     */
    @Override
    public Boolean addMembercardUser(SMemberCard sMemberCard, String openId) {
        List<SMembercardUser> data=this.getMembercardUser(openId);
        if(data==null||data.size()==0){
            createMembercardUser(sMemberCard,new Date(),openId);
        }else{
            SMembercardUser lastMembercardUser=data.get(0);
            createMembercardUser(sMemberCard,lastMembercardUser.getEndTime(),openId);
        }
        return true;
    }

    /**
     * 获取当前时间的有效会员信息
     */
    @Override
    public SMembercardUser getCurrentMembercardUser() {
        SUser sysUser = (SUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("open_id",sysUser.getOpenid());
        queryWrapper.ge("start_time",new Date());
        queryWrapper.orderByAsc("end_time");
        List<SMembercardUser> data=this.list(queryWrapper);
        if(data!=null&&data.size()>0){
            return data.get(0);
        }
        return null;
    }

    /**
     * 创建用户会员卡信息
     * @param sMemberCard
     * @param startDate
     * @param openId
     */
    public void createMembercardUser(SMemberCard sMemberCard,Date startDate,String openId){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        Date endTime=null;
        if(BookConstant.TIME_UNIT_WEEK.equals(sMemberCard.getValidTimeUnit())){
            calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+7*sMemberCard.getValidTime()); //给当前时间增加周
            calendar= DayUtil.setLastHourMinite(calendar);
            endTime = calendar.getTime();
        }else if(BookConstant.TIME_UNIT_MONTH.equals(sMemberCard.getValidTimeUnit())){
            calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1*sMemberCard.getValidTime()); //增加月
            calendar=DayUtil.setLastHourMinite(calendar);
            endTime = calendar.getTime();
        }else if(BookConstant.TIME_UNIT_QUARTER.equals(sMemberCard.getValidTimeUnit())){
            calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+3*sMemberCard.getValidTime()); //增加季度
            calendar=DayUtil.setLastHourMinite(calendar);
            endTime = calendar.getTime();
        }else if(BookConstant.TIME_UNIT_YEAR.equals(sMemberCard.getValidTimeUnit())){
            calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)+1*sMemberCard.getValidTime()); //增加年
            calendar=DayUtil.setLastHourMinite(calendar);
            endTime=calendar.getTime();
        }
        //新增绑定会员信息
        SMembercardUser  memcardUser=new SMembercardUser();
        memcardUser.setStartTime(startDate);
        memcardUser.setCreateTime(new Date());
        memcardUser.setCardId(sMemberCard.getId());
        memcardUser.setOpenId(openId);
        memcardUser.setEndTime(endTime);
        memcardUser.setStatus(BookConstant.MEMCARD_STATUS_VALID);
        memcardUser.setBorrowTimes(sMemberCard.getBorrowTimes());
        this.save(memcardUser);
    }
}
